<?php
namespace xcom\model;
use x\Xbase;
class StepToken extends Xbase
{
    /**
     * 创建token
     * @return [type] [description]
     */
    public function create(){
        $this->db->query('delete from step_token where create_time<?',[time()-30*60]);
        $token = token();
        $d = [
            'token' => $token,
            'ip' => ip(),
            'end_time' => time()+5*60,
            'create_time' => time(),
        ];
        $this->db->insert('step_token',$d);
        return $token;
    }

    /**
     * 验证上一步下一步token
     * @param  [type] $step_token [token]
     * @return [type]        [description]
     */
    public function checkToken($step_token){
        $rs = $this->db->query('select * from step_token where token=?',[$step_token],1);
        if($rs){
            $this->db->delete('step_token',$rs['id']);
            if($rs['end_time']<time()){
                $this->setError('验证token过期,请重新回到上一步');
                return false;
            }
            return true;
        }
        $this->setError('上一步验证token无效');
        return false;
    }
}
